<?php
require_once "redis.class.php";
require_once "util.php";
require_once 'connection.class.php';

class Record
{
	private $_redis;
	private $_timeFrame;
	private $_hit = array();
	private $_hitId;
	
	
	public function __construct($uid = '', $context = null){
		if ($this->_redis == null)
		{
			try{
				$this->_redis = RedisConnection::getRedis();
			} catch (RedisException $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		if(!empty($uid))
		{
			$userInfo = $this->getRecordingInfo($uid);
			if ($userInfo == false)
			{
				return ;
			}
			$this->_hitId = $this->_redis->incr('global:id:hit');
			$this->_hit['ip'] = Util::getClientIp();
			$this->_hit['session'] = session_id();
			$this->_hit['user'] = $uid;
			$this->_hit['cam'] = !empty($userInfo['cam']) ? $userInfo['cam'] : '';
			$this->_hit['cre'] = !empty($userInfo['cre']) ? $userInfo['cre'] : '';
			$this->_hit['lu'] = !empty($userInfo['lu']) ? $userInfo['lu'] : '';
			$this->_hit['pub'] = empty($userInfo['pub'])? '': $userInfo['pub'];
			$this->_hit['pla'] = empty($userInfo['pla']) ? '': $userInfo['pla'];
			$this->_hit['buy_type'] = empty($userInfo['buy_type']) ? '' : $userInfo['buy_type'];
			$this->_hit['media_type'] = empty($userInfo['media_type']) ? '' : $userInfo['media_type'];
			$this->_hit['offer'] = empty($userInfo['offer']) ? '' : $userInfo['offer'];
			$this->_hit['site'] = $userInfo['domain'];
			$this->_hit['game'] = Util::getGame($userInfo['game']);
			$this->_hit['time'] = time();
		}
		if($uid == '' && $context !== null)
		{
			$this->_hitId = $this->_redis->incr('global:id:hit');
			$this->_hit['ip'] = Util::getClientIp();
			$this->_hit['session'] = session_id();
			$this->_hit['user'] = '';
			$this->_hit['cam'] = $context->get('cam', '');
			$this->_hit['cre'] = '';
			$this->_hit['lu'] = $context->get('lu', '');
			$this->_hit['pub'] = $context->get('pub', '');
			$this->_hit['pla'] = $context->get('pla', '');
			$this->_hit['buy_type'] = '';
			$this->_hit['media_type'] = '';
			$this->_hit['offer'] = '';
			$this->_hit['site'] = $context->_domain;
			$this->_hit['game'] = Util::getGame($context->_game);
			$this->_hit['time'] = time();
		}
		
		$this->_timeFrame = Util::getFrameTime(time());
	}
	
	public function getRecordingInfo($uid)
	{
		$dbconn = Connection::getInstance();
		try
		{
			$sql = 'select u.cam, u.pub, u.pla, u.cus, u.cre, u.lu, u.buy_type, u.media_type, u.offer, s.domain, s.game  from users u left join sites s on u.site_id = s.id where u.id=?';
			$result = $dbconn->query($sql, array($uid));
			if (count($result) == 0)
			{
				return false;
			}
			else
			{
				return $result[0];	
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function addPlayRecording($won, $credit, $bet = 0){
		
		$this->_hit['won'] = $won;
		$this->_hit['bet'] = $bet;
		$this->_hit['credit'] = $credit;
		$this->_hit['event'] = 'played';
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
		
//		QLog::log('hit:'.$this->_hitId . "\t" . serialize($this->_redis->lRange('timeframe:'.$this->_timeFrame, 0, -1)) . "\n", QLog::INFO);
	}
	
	public function addOfferCallbackRecording($reward, $revenue, $monetizationName){
		$this->_hit['token'] = $reward;
		$this->_hit['event'] = 'reward';
		$this->_hit['revenue'] = $revenue;
		$this->_hit['monetization'] = $monetizationName;
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addLandingRecording(){
		$this->_hit['event'] = 'landing';
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addBasicRegRecording($credit, $monetization=''){
		$this->_hit['credit'] = $credit;
		$this->_hit['event'] = 'basic_reg';
		if (!empty($monetization))
		{
			$this->_hit['monetization'] = $monetization;
		}	
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addFullRegRecording($credit, $monetization=''){
		$this->_hit['credit'] = $credit;		
		$this->_hit['event'] = 'full_reg';
		$this->_hit['monetization'] = $monetization;
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addConfirmRecording($credit, $monetization='')
	{
		$this->_hit['credit'] = $credit;		
		$this->_hit['event'] = 'confirmed';
		$this->_hit['monetization'] = $monetization;
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}

	public function addFreeCreditsRecording($credit){
		$this->_hit['credit'] = $credit;
		$this->_hit['event'] = 'free';
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addHomeViewRecording(){
		$this->_hit['event'] = 'home_view';
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addMoreCreditsViewRecording($monetizations){
		$this->_hit['event'] = 'more_credit';
		$this->_hit['offer_wall1'] = $monetizations[0];
		$this->_hit['offer_wall2'] = $monetizations[1];
		$this->_hit['offer_wall3'] = $monetizations[2];
		$this->_hit['offer_wall4'] = $monetizations[3];
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addCoregRecording($event, $monetization=''){
		$this->_hit['event'] = $event;
		if (!empty($monetization))
		{
			$this->_hit['monetization'] = $monetization;
		}
		
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addSuppressedPixelRecording(){
		$this->_hit['event'] = 'suppress';
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addShareRecording($shareType, $shareCountsField, $shareCounts){
		$this->_hit['event'] = $shareType;
		$this->_hit[$shareCountsField] = $shareCounts;
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addRefEmailOpen() {
		$this->_hit['cam'] = 'ref';
		$this->_hit['pub'] = 'em';
		$this->_hit['event'] = 'ref_email_open';
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addBlueBarOfferClickRecording($bluebar_offer){
		$this->_hit['event'] = 'bluebar_click';
		$this->_hit['bluebar_offer'] = $bluebar_offer;
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addBlueBarOfferCompleteRecording($bluebar_offer){
		$this->_hit['event'] = 'bluebar_complete';
		$this->_hit['bluebar_offer'] = $bluebar_offer;
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addEmailSentRecording($email_type, $email_offer_tosentdate = ''){
		$this->_hit['event'] = 'email_sent';
		$this->_hit['email_type'] = $email_type;
		$this->_hit['email_offer_tosentdate'] = $email_offer_tosentdate;
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addEmailOpenRecording($email_type, $email_offer_tosentdate = ''){
		$this->_hit['event'] = 'email_open';
		$this->_hit['email_type'] = $email_type;
		$this->_hit['email_offer_tosentdate'] = $email_offer_tosentdate;
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function addEmailOfferCompleteRecording($email_type, $email_offer_tosentdate = ''){
		$this->_hit['event'] = 'email_offer_complete';
		$this->_hit['email_type'] = $email_type;
		$this->_hit['email_offer_tosentdate'] = $email_offer_tosentdate;
		$this->_redis->hMset('hit:'.$this->_hitId, $this->_hit);
		$this->_redis->rPush('timeframe:'.$this->_timeFrame, 'hit:'.$this->_hitId);
	}
	
	public function __destruct(){
		//$this->_redis->close();
	}
}
?>